/**
 * @Author : woon
 * @Timestamp : 2016-06-29
 */

var page = sm("do_Page");
var app = sm("do_App");
var nf = sm("do_Notification");
var hashdata = mm("do_HashData");
var circleUrl = require("url/circle");
var http = require("helper/http");
var httpSilence = require("helper/httpSilence");
var openHelper = require("helper/open");
var barcodeHelper = require("helper/barcode");
var bizzType = require("helper/bizzType");
var helperVote = require("helper/vote");
var userUtil = require("helper/user");

var listview = ui("ListView");
var listdata = mm("do_ListData");
listview.bindItems(listdata);//绑定item的数据
var noData = [{"template":4,"labelContent":"该圈子没有话题"}];

var httpUrl4Detail = circleUrl.url.base.detail;
var httpUrl4TopicList = circleUrl.url.topic.list4circle;

/**模板列表
source://view/circle/topic/topicCell.ui
source://view/circle/base/detail/circleDetailCell1.ui
source://view/activity/list/circle/listCell.ui
source://view/promotion/listCell.ui
source://view/reuse/noCell.ui
source://view/circle/topic/topicCell2top.ui
 */
//上个页面转过来的参数
var circleId = null;
var circleName=null;
var circleLogo=null;
var auditStatus = null;//1-待审核;2-审核通过;3-审核不通过'
//当前页码
var pageNum = 1;
//是否最后一页

//andriod返回键
page.on("back", function() {
    app.closePage();
});

//从新增页面返回,刷新数据
page.on("result", function(data){
    if (data.topic_add || data.topic_add_vote|| data.add_activity) {
    	loadFirstPage(true);
    }
    if(data.isCircleTopicUpdate){
    	for (var i = 1; i < listdata.getCount(); i++) {//从第2行开始，跳过第一行【圈子详情】
    		var oneData = listdata.getOne(i);
    		if (oneData.id==data.topicId) {
    			oneData.commentCount = data.commentCount;
    			oneData.praiseCount = data.praiseCount;
    			oneData.isPraised = data.isPraised;
    			listdata.updateOne(i, oneData);
    			listview.refreshItems();
    			break;
    		}
    	}
    }
    if(data.topic_delete){
    	for (var i = 1; i < listdata.getCount(); i++) {//从第2行开始，跳过第一行【圈子详情】
    		var oneData = listdata.getOne(i);
    		if (oneData.id==data.topicId) {
    			listdata.removeData([i]);
    			listview.refreshItems();
    			break;
    		}
    	}
		nf.toast("删除话题成功!");
	}
	if(data.activity_delete){
		loadFirstPage(true);
		nf.toast("删除活动成功!");
	}
});

//签到成功后-刷新数据
page.on("circle_sign_success", function(data){
	var retCircleId = data;
	if(circleId == retCircleId){
		var circleData = listdata.getOne(0);//圈子detail
		circleData.canSign = false;
		listdata.updateOne(0, circleData);//更新圈子detail数据
	}
});

//成功加入圈子
page.on("join_circle_success", function(data){
	setCircleInfo4newMember();
});
/****************处理页面-头部-begin*****************/
var headerAout = ui("headerAout");
var addheader = headerAout.add("header", "source://view/reuse/header.ui", 0, 0);

//左上角-返回
var headerLeft = ui(addheader+".headerLeft");
headerLeft.on("touch", function() {
	//传入参数work_add:true,为了让下层页面result刷新
	app.closePage({work_reflesh:false});
});

var labelTitle = ui(addheader+".labelTitle");

//右上角-创建按钮
var headerRight4Add = ui(addheader+".headerRight");
var imgNext = ui(addheader+".imgNext");
imgNext.source="source://image/common/more.png";//添加话题-图标
/*
 * 菜单弹出
 */
var menus = [];

function setMenu(vsCanManage){
	menus = [ {
		"template" : 0,
		"menuIcon" : "source://image/menu/edit.png",
		"menuName" : "发布动态",
		"type" : "addTopic"
	}];
	if(memberType==1 || memberType==2){//圈主才能或管理员才能发布活动
		menus.push({
			"template" : 0,
			"menuIcon" : "source://image/menu/activity.png",
			"menuName" : "发布活动",
			"type" : "addActivity"
		});
	}
	if(canAddVote==1){
		menus.push({
			"template" : 0,
			"menuIcon" : "source://image/menu/vote.png",
			"menuName" : "发布投票",
			"type" : "addVote"
		});
	}
	if(vsCanManage){
		menus.push({
			"template" : 0,
			"menuIcon" : "source://image/setting/setting.png",
			"menuName" : "管理圈子",
			"type" : "circleManage"
		});
	}
	if(memberType==1){//圈主才能管理话题
		menus.push({
			"template" : 0,
			"menuIcon" : "source://image/setting/setting.png",
			"menuName" : "管理话题",
			"type" : "topicManage"
		});
	}
	if(memberType==2 || memberType==3){//普通成员 或管理员才能退出圈子
		menus.push({
			"template" : 0,
			"menuIcon" : "source://image/common/quit.png",
			"menuName" : "退出圈子",
			"type" : "quitCircle"
		});
	}
	if(memberType==1 || memberType==2 || memberType==3){//圈子成员 才能打开二维码
		menus.push({
			"template" : 0,
			"menuIcon" : "source://image/find/qrcode2.png",
			"menuName" : "二维码",
			"type" : "qrCode"
		});
	}
	
}
var popmenu = ui("ALayout_root").add("menupop", "source://view/reuse/menu/menu.ui", 0, 0);
var popmenuf = ui("menupop");
headerRight4Add.on("touch", function() {
	popmenuf.fire("popmenuSJ", menus);
});

popmenuf.on("popmenuItem", function(data) {
	popmenuf.fire("popmenuClose");// 关闭菜单
	var touchIndex = parseInt(data);
	var actionType = menus[touchIndex].type;
	
	if(canAddTopic){
		if (actionType == "addTopic") {// 发话题
			if(auditStatus!="2"){
				nf.toast("圈子未通过审核!");
				return;
			}
			openHelper.open("source://view/circle/topic/manage/topicAdd.ui",{circleId:circleId});
		}else if (actionType == "addActivity") {// 发活动
			if(auditStatus!="2"){
				nf.toast("圈子未通过审核!");
				return;
			}
			openHelper.open("source://view/activity/add/circle/mainNew.ui",{circleId:circleId,circleName:circleName});
		}else if (actionType == "addVote") {// 发投票
			if(auditStatus!="2"){
				nf.toast("圈子未通过审核!");
				return;
			}
			openHelper.open("source://view/vote/add/main.ui",{circleId:circleId,bizzType:helperVote.bizzType.CIRCLE});
		}else if (actionType == "qrCode") {// 圈子二维码
			barcodeHelper.toQrCodeImg(bizzType.Level1.CIRCLE,circleId,circleLogo,circleName,"");
		}else if(actionType == "circleManage"){//管理圈子
			openHelper.open("source://view/circle/base/manageOne/main.ui",{circleId:circleId});//新的
		}else if(actionType == "quitCircle"){//退出圈子
			quitOffCircle();
		}else if(actionType == "topicManage"){//管理话题
			openHelper.open("source://view/circle/topic/manage/topic4circle.ui",{circleId:circleId});
		}
	}else{
		nf.toast("您不是该圈子成员或已被管理员屏蔽!");
	}
});
var header = ui("header");
header.bindData(hashdata);
/****************处理页面-头部-end*****************/

var canAddTopic = false;//是否可以添加话题
page.on("loaded", function(){
	circleId = page.getData().circleId;
	circleName = page.getData().circleName;
	labelTitle.text = circleName;
	getCircleInfo();
});
var canAddVote = 2;//是否可以添加投票
var canAddActivity = 2;//是否可以添加活动
var memberType = 99;//圈子成员类型：1-圈主;2-管理员;3-普通成员
function getCircleInfo(){
	httpSilence.post(httpUrl4Detail,{id:circleId},function(data){
		var circle = data.circleBase;
		var canManage = data.canManage;//是否可以管理这个圈子
		if(circle){
			auditStatus=circle.auditStatus;
			circleName =circle.name;
			labelTitle.text = circle.name;
			circleLogo=circle.fullLogo;
			canAddTopic = circle.canAddTopic;
			if(data.circleMemberVo){
				canAddVote=data.circleMemberVo.canAddVote;
				canAddActivity=data.circleMemberVo.canAddActivity;
				memberType = data.circleMemberVo.type;
				circle.isShowSign = true;//如果是成员，才显示签按钮
			}else{
				circle.isShowSign = false;//不是成员，不显示签按钮
			}
			circle.template = 1;//指定模板,加载圈子说情
			listdata.removeAll();
			listdata.addOne(circle);//插入一条数据【圈子详情】
			listview.refreshItems();//刷新item数据
			loadFirstPage(false);
			if(canAddTopic){
				imgNext.visible = true;
				headerRight4Add.visible = true;
			}else{
				imgNext.visible = false;
				headerRight4Add.visible = false;
			}
		}
		setMenu(canManage);//设置右上角菜单项
	});
}

function setCircleInfo4newMember(){
	memberType = 3;//普通成员
	canAddTopic = true;
	canAddVote = true;
	imgNext.visible = true;
	headerRight4Add.visible = true;
	setMenu(false);//设置右上角菜单项
	
	var circleData = listdata.getOne(0);//圈子detail
	circleData.canSign = true;
	circleData.isShowSign = true;
	circleData.canChat = true;
	listdata.updateOne(0, circleData);//更新圈子detail数据
	listview.refreshItems();
}
//全部刷新数据
/**
 * 
 * @param isSilence 【是否静默方式请求后台】
 * @returns
 */
function loadFirstPage(isSilence){
	var httpHander = http;
	if(isSilence){
		httpHander = httpSilence;
	}
	pageNum=1;
	httpHander.post(httpUrl4TopicList,{circleId:circleId,page:pageNum},function(data){
		if(data.topicPage){
			if(listdata.getCount()>1){
				//清除已经加载了话题,把话题列表数据清除
				listdata.removeRange(1, listdata.getCount()-1);
			}
			
			var topicList4Top = data.topicList4Top;
			if(topicList4Top && topicList4Top.length>0){
				for(var i in topicList4Top){
					topicList4Top[i].template =5;//指定模板【置顶话题】
					listdata.addOne(topicList4Top[i]);
				}
			}
			var activityBaseVo = data.activityBaseVo;
			if(activityBaseVo){
				activityBaseVo.template =2;//指定模板
				listdata.addOne(activityBaseVo);
			}
			
			if(data.topicPage.content.length){
				listdata.addData(data.topicPage.content);
			}
			if(data.isPromotion){
				listdata.addData([{"template": 3,"promotionList":data.promotionList}]);
			}
			listview.refreshItems();
		}
	});
}

function loadNextPage(){
	pageNum++;
	http.post(httpUrl4TopicList,{circleId:circleId,page:pageNum},function(data){
		if(data.topicPage){
			var topicListNext = data.topicPage.content;
			if(topicListNext && topicListNext.length>0){
				var length = listdata.getCount()-1;
				deviceone.print(JSON.stringify(data.topicPage.content))
				listdata.addData(data.topicPage.content,length);
				listview.refreshItems();
			}
		}
	});
}

//下拉列表，刷新数据
listview.on("pull", function(data){
	if (data.state == 2){
		loadFirstPage(false);
		listview.rebound();//复位【一定要做】
	}
});

//上拉列表，翻页数据
listview.on("push", function(data){
	if (data.state == 2){
		loadNextPage();
		listview.rebound();//复位【一定要做】
	}
});

//点击一条话题
listview.on("touch", function(data){
	var onTopic = listdata.getOne(data);
	var topicId = onTopic.id;
	if(onTopic.template==1){
		return;//点击的是圈子信息
	}
	if(onTopic.template==3){//点击的活动
		openHelper.open("source://view/activity/detail/msgBorderView.ui",{id:topicId},"","pay_product_back");
	}
	if(onTopic.template==''||onTopic.template==null){//点击的话题
		openHelper.open("source://view/circle/topic/detail/topicBorderView.ui",{topicId:topicId});
	}
	if(onTopic.template==5){//点击的 置顶话题
		//bizzType:1-话题;2-投票;3-活动',
		if(onTopic.bizzType=="1" || onTopic.bizzType=="2"){
			openHelper.open("source://view/circle/topic/detail/topicBorderView.ui",{topicId:topicId});
		}else if(onTopic.bizzType=="3"){
			openHelper.open("source://view/activity/detail/msgBorderView.ui",{id:topicId},"","pay_product_back");
		}
	}
});

//退出圈子
function quitOffCircle(){
	if(userUtil.loginToken()===""){
		nf.alert("您还没有登录哦.");
	}else{
		http.post(circleUrl.url.member.quitCircle,{"circleId":circleId},function(data){
			nf.toast("成功退出");
			app.closePage({quit_circle:true});
		});
	}
}